<template>
  <el-card>
    <!-- 1.0 标题 -->
    <template #header>
      商品添加
    </template>
    <!-- 2.0 内容 -->
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
      size="mini"
    >
      <el-form-item label="商品名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="商品分类" prop="category">
        <el-select v-model="ruleForm.category" placeholder="请选择">
          <el-option
            v-for="item in cateName"
            :key="item.cateName"
            :label="item.cateName"
            :value="item.cateName"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品价格" prop="price">
        <template>
          <el-input-number
            v-model="ruleForm.price"
            :min="1"
            :max="10"
            label="描述文字"
          ></el-input-number>
        </template>
      </el-form-item>

      <el-form-item label="商品图片" prop="imgUrl">
        <el-upload
          class="avatar-uploader"
          action="http://127.0.0.1:5000/goods/goods_img_upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="商品描述" prop="goodsDesc">
        <el-input type="textarea" v-model="ruleForm.goodsDesc"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >立即创建</el-button
        >
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import { goodadd, categoriesgoods, goodsimg } from '../../api/goods'
export default {
  data() {
    return {
      cateName: [],
      dialogImageUrl: '',
      dialogVisible: false,
      imageUrl: '',
      imgurl: '',
      ruleForm: {
        name: '',
        category: '',
        price: '',
        goodsDesc: ''
      },
      rules: {
        name: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
        category: [{ required: true, message: '请选择', trigger: 'change' }],
        price: [{ required: true, message: '请输入', trigger: 'change' }],
        goodsDesc: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    }
  },
  methods: {
    /* 查询所有分类名称 */
    async fen() {
      const { categories } = await categoriesgoods()
      this.cateName = categories
    },
    /* 添加 */
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          const { code, msg } = await goodadd({
            name: this.ruleForm.name,
            category: this.ruleForm.category,
            price: this.ruleForm.price,
            imgUrl: this.imgurl,
            goodsDesc: this.ruleForm.goodsDesc
          })
          if (code === 0) {
            this.$router.push('/goods/list')
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.imgurl = res.imgUrl
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }
  },
  created() {
    this.fen()
  }
}
</script>

<style lang="less" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 138px;
  height: 138px;
  line-height: 138px;
  text-align: center;
}
.avatar {
  width: 138px;
  height: 138px;
  display: block;
}
/deep/.avatar-uploader-icon {
  border: 1px solid #ccc;
  border-radius: 6px;
}
/deep/.el-textarea__inne {
  width: 178px;
}
.el-input {
  width: 178px;
}
/deep/.el-textarea {
  width: 178px;
}
</style>
